package com.yc.demo1.d0301;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import com.yc.hr.dao.DeptDao;

import scott.util.JdbcTemplate;

import javax.swing.JButton;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class QueryDeptWin {

	private JFrame frame;
	private JTextField textField;
	private JTextField textField_1;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					QueryDeptWin window = new QueryDeptWin();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public QueryDeptWin() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		flowLayout.setHgap(10);
		frame.getContentPane().add(panel, BorderLayout.NORTH);
		
		JLabel lblNewLabel = new JLabel("部门:");
		panel.add(lblNewLabel);
		
		textField = new JTextField();
		panel.add(textField);
		textField.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("位置:");
		panel.add(lblNewLabel_1);
		
		textField_1 = new JTextField();
		panel.add(textField_1);
		textField_1.setColumns(10);
		
		JButton btnNewButton = new JButton("查询");
		panel.add(btnNewButton);
		
		JScrollPane scrollPane = new JScrollPane();
		frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
		
		table = new JTable();
		scrollPane.setViewportView(table);
		
		//////////////////////////////////////////////////
		Vector<String> head = new Vector<>();
		head.add("部门编号");
		head.add("部门名称");
		head.add("部门位置");
		Vector<Vector<Object>> data = new Vector<>();
		
		TableModel model = new DefaultTableModel(data, head);
		table.setModel(model);
		
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				String sql = "select * from dept where 1=1";
//				List<Object> params = new ArrayList<>();
//				if(textField.getText().isBlank() == false) {
//					sql += " and dname like '%'||?||'%'";
//					params.add(textField.getText().trim());
//				}
//				if(textField_1.getText().isBlank() == false) {
//					sql += " and loc like '%'||?||'%'";
//					params.add(textField_1.getText().trim());
//				}
//				// 执行查询
//				List<Map<String,Object>> list = JdbcTemplate.queryList(sql, params.toArray());
				
				DeptDao dao = new DeptDao();
				List<Map<String,Object>> list = dao.queryDnameAndLoc(textField.getText(), textField_1.getText());
				// 清空data中原有的数据
				data.clear();
				for(int i=0; i<list.size();i++) {
					Vector<Object> row = new Vector<>();
					row.add(list.get(i).get("DEPTNO"));
					row.add(list.get(i).get("DNAME"));
					row.add(list.get(i).get("LOC"));
					data.add(row);
				}
				table.updateUI();
			}
			
			
			
		});
		
		
		
		
		
		
		
		
		
		
		
		
		
	}

}
